import { ApiProperty } from '@nestjs/swagger';
import { Expose, Transform } from 'class-transformer';
import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  Index,
  Generated,
  CreateDateColumn,
  TreeChildren,
  TreeParent,
  UpdateDateColumn,
  OneToMany,
  PrimaryColumn,
} from 'typeorm';
import { RoleMenu } from './role-menu.entity';
import * as crypto from 'crypto';
import { BaseEntity } from './base.entity';
@Entity('menu') // 表名为 'menu'
@Index('idx_sp_parent_id', ['parentId'])
@Index('idx_sp_is_route', ['isRoute'])
@Index('idx_sp_is_leaf', ['isLeaf'])
@Index('idx_sp_sort_no', ['sortNo'])
@Index('idx_sp_del_flag', ['delFlag'])
@Index('idx_sp_menu_type', ['menuType'])
@Index('idx_sp_hidden', ['hidden'])
@Index('idx_sp_status', ['status'])
export class Menu extends BaseEntity {
  constructor() {
    super();
    this.id = crypto.randomBytes(8).toString('hex'); // 生成一个8字节（16个十六进制字符）的随机字符串作为id
  }
  @ApiProperty({
    required: false,
    description: '自增唯一主键',
  })
  @PrimaryColumn({ length: 225 })
  id: string = ' ';

  @ApiProperty({
    required: false,
    description: '父id',
  })
  @Column('varchar', { nullable: true, comment: '父id' })
  parentId: string = ' ';

  @ApiProperty({
    required: false,
    description: '菜单标题',
  })
  @Column('varchar', { length: 255, nullable: true, comment: '菜单标题' })
  name: string = ' ';

  @ApiProperty({
    required: false,
    description: '路径',
  })
  @Column('varchar', { length: 255, nullable: true, comment: '路径' })
  url: string = ' ';

  @ApiProperty({
    required: false,
    description: '组件',
  })
  @Column('varchar', { length: 255, nullable: true, comment: '组件' })
  component: string = ' ';

  @ApiProperty({
    required: false,
    description: '是否路由菜单: 0:不是 1:是（默认值1）',
  })
  @Column('tinyint', {
    nullable: true,
    comment: '是否路由菜单: 0:不是 1:是（默认值1）',
  })
  @Expose({ name: 'isRoute' })
  @Transform(({ value }) => value === 1)
  isRoute: number;

  @ApiProperty({
    required: false,
    description: '组件名字',
  })
  @Column('varchar', { length: 255, nullable: true, comment: '组件名字' })
  componentName: string = ' ';

  @ApiProperty({
    required: false,
    description: '一级菜单跳转地址',
  })
  @Column('varchar', {
    length: 255,
    nullable: true,
    comment: '一级菜单跳转地址',
  })
  redirect: string = ' ';

  @ApiProperty({
    required: false,
    description: '菜单类型(0:一级菜单; 1:子菜单:2:按钮权限)',
  })
  @Column('int', {
    nullable: true,
    comment: '菜单类型(0:一级菜单; 1:子菜单:2:按钮权限)',
  })
  menuType: number;

  @ApiProperty({
    required: false,
    description: '菜单权限编码',
  })
  @Column('varchar', { length: 255, nullable: true, comment: '菜单权限编码' })
  perms: string = ' ';

  @ApiProperty({
    required: false,
    description: '权限策略1显示2禁用',
  })
  @Column('varchar', {
    length: 10,
    nullable: true,
    default: '0',
    comment: '权限策略1显示2禁用',
  })
  @Expose({ name: 'permsType' })
  @Transform(({ value }) => value === 1)
  permsType: string = ' ';

  @ApiProperty({
    required: false,
    description: '菜单排序',
  })
  @Column({ nullable: true, comment: '菜单排序' })
  sortNo: number;

  @ApiProperty({
    required: false,
    description: '菜单图标',
  })
  @Column('varchar', { length: 255, nullable: true, comment: '菜单图标' })
  icon: string = ' ';

  @ApiProperty({
    required: false,
    description: '是否叶子节点: 1:是 0:不是',
  })
  @Column('tinyint', {
    nullable: true,
    default: 1,
    comment: '是否叶子节点: 1:是 0:不是',
  })
  @Expose({ name: 'isLeaf' })
  @Transform(({ value }) => value === 1)
  isLeaf: number;

  @ApiProperty({
    required: false,
    description: '是否缓存该页面: 1:是 0:不是',
  })
  @Column('tinyint', {
    nullable: true,
    comment: '是否缓存该页面: 1:是 0:不是',
  })
  @Expose({ name: 'keepAlive' })
  @Transform(({ value }) => value === 1)
  keepAlive: number;

  @ApiProperty({
    required: false,
    description: '是否隐藏路由: 0否,1是',
  })
  @Column('int', {
    nullable: true,
    default: 0,
    comment: '是否隐藏路由: 0否,1是',
  })
  @Expose({ name: 'hidden' })
  @Transform(({ value }) => value === 1)
  hidden: number;

  @ApiProperty({
    required: false,
    description: '是否隐藏tab: 0否,1是',
  })
  @Column('tinyint', { nullable: true, comment: '是否隐藏tab: 0否,1是' })
  @Expose({ name: 'hideTab' })
  @Transform(({ value }) => value === 1)
  hideTab: number;

  @ApiProperty({
    required: false,
    description: '描述',
  })
  @Column('varchar', { length: 255, nullable: true, comment: '描述' })
  description: string = ' ';

  @ApiProperty({
    required: false,
    description: '创建人',
  })
  @Column('varchar', { length: 255, nullable: true, comment: '创建人' })
  createBy: string = ' ';

  @ApiProperty({
    required: false,
    description: '删除状态 0正常 1已删除',
  })
  @Column('int', {
    nullable: true,
    default: 0,
    comment: '删除状态 0正常 1已删除',
  })
  @Expose({ name: 'delFlag' })
  @Transform(({ value }) => value === 1)
  delFlag: number;

  @ApiProperty({
    required: false,
    description: '按钮权限状态(0无效1有效)',
  })
  @Column('varchar', {
    length: 2,
    nullable: true,
    comment: '按钮权限状态(0无效1有效)',
  })
  @Expose({ name: 'status' })
  @Transform(({ value }) => value === 1)
  status: string = ' ';

  @TreeParent({
    onDelete: 'CASCADE', // 自动保存相关对象
  })
  parent?: Menu;

  @TreeChildren({
    cascade: true, // 自动保存相关对象
  })
  children?: Menu[];

  @OneToMany(() => RoleMenu, (roleMenu) => roleMenu.menuId)
  roleMenus: RoleMenu[];
}
